package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHSWUtil
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import org.json.JSONObject
import org.json.JSONArray
import org.json.JSONException
import org.springframework.jdbc.core.JdbcTemplate

import java.text.SimpleDateFormat

/**
 *
 * @Title: auto_SZWaterA00015DBresult
 * * @Description: 功能介绍：定时调用此接口，将短信账单短信documentout_ucis中符合条件的短信调度短信平台接口获取短信结果
 * @author: chieh
 * @date 2022-02-10 10:28:00
 *
 */

public class auto_SZWaterA00015DBresult implements EliteThreadInvoke {
    private static final Log log = LogFactory.getLog(auto_SZWaterA00015DBresult.class);

    public int doCallBack(EliteBean eliteBean, String content) {

        try {
            def SMSCount = 0;
            def msg;
            def message;
            def outdata;
            def updateDocumentoutSQL;
            String sms_guid = content;
            log.debug("水费账单短信结果捞取的批次号smsguid：" + sms_guid);

            if (sms_guid != "" && sms_guid != null) {
                //更新监控表 捞取短信的时间
                eliteBean.update("Update Ucis_Sms_Monitoring Set Smsresult_Starttime = Getdate() Where Sms_Guid = '" + sms_guid + "' And Smsresult_Starttime Is Null", "dataSource")

                String dealTimeOutSQL = '''Update Documentout_Ucis
                                               Set Documentout_Ucis.Result_Code = 'SMSFAIL',
                                                   Documentout_Ucis.Result_Msg  = '超出计划设定短信回写时间强制发送失败',
                                                   Documentout_Ucis.Result_Time = Getdate(),
                                                   Documentout_Ucis.Smsstatus   = 'SMSFAIL\'
                                               From Documentout_Ucis 
                                             Inner Join Ucis_Smssendplan 
                                               On Documentout_Ucis.Sms_Guid = Ucis_Smssendplan.Rec_Guid
                                             Where Documentout_Ucis.Sms_Guid = \'''' + sms_guid + '''\'
                                               And Ucis_Smssendplan.Rec_Guid = \'''' + sms_guid + '''\'
                                               And Documentout_Ucis.Status = 3
                                               And Documentout_Ucis.Send_Time < Dateadd(Minute, -Isnull(Ucis_Smssendplan.Sms_Jssc, 300), Getdate())
                                               And (Documentout_Ucis.Smsstatus Is Null Or Documentout_Ucis.Smsstatus = '')''';
                log.debug("处理已经超时未获取到短信结果的数据SQL：" + dealTimeOutSQL);
                int dealTimeOutNum = eliteBean.update(dealTimeOutSQL, "dataSource");
                log.debug("处理已经超时未获取到短信结果的数据SQL 操作数据量：" + dealTimeOutNum);

                if (dealTimeOutNum > 0) {
                    String batchResult = '''Insert Into Ucis_Proc_Sendsmsresult
                                  (Rec_Guid, Send_Guid, Send_Time, Sms_Guid, Sync_Time, Smsstatus)
                                  Select Newid(), R1.Documentout_Guid, R1.Send_Time, R1.Sms_Guid, Getdate(), R1.Smsstatus
                                    From Documentout_Ucis R1
                                    Left Join Ucis_Proc_Sendsmsresult R2
                                      On R1.Documentout_Guid = R2.Send_Guid
                                   Where R1.Sms_Guid = \'''' + sms_guid + '''\'
                                     And R2.Send_Guid Is Null''';
                    log.debug("处理已经超时未获取到短信结果2的数据SQL：" + batchResult);
                    int batchResultNum = eliteBean.update(batchResult, "dataSource");
                    log.debug("处理已经超时未获取到短信结果2的数据SQL 操作数据量：" + batchResultNum);
                }

                //查询任务开关是否有数据
                int Send_Num = 0;
                int Result_Num = 0;
                int No_Result_Num = 0;
                int Result_Succ_Num = 0;
                String selectSendSQL = '''Select Count(*) As Send_Num,
                                               Sum(Case
                                                     When Smsstatus Is Not Null And Smsstatus != '' Then
                                                      1
                                                     Else
                                                      0
                                                   End) As Result_Num,
                                               Sum(Case
                                                     When Smsstatus Is Null Or Smsstatus = '' Then
                                                      1
                                                     Else
                                                      0
                                                   End) As No_Result_Num,
                                               Sum(Case
                                                     When Smsstatus = 'DELIVRD' Then
                                                      1
                                                     Else
                                                      0
                                                   End) As Result_Succ_Num
                                          From Documentout_Ucis
                                         Where (Status = 3 Or Status = -1)
                                           And Doctype = '3'
                                           And Isnumeric(Documentto) = 1
                                           And Resultcode Is Not Null
                                           And Sms_Guid = \'''' + sms_guid + '''\'''';
                List selectSendList = eliteBean.chaxun_sql(selectSendSQL, "dataSource");
                Send_Num = selectSendList.get(0).get("Send_Num");
                Result_Num = selectSendList.get(0).get("Result_Num");
                No_Result_Num = selectSendList.get(0).get("No_Result_Num");
                Result_Succ_Num = selectSendList.get(0).get("Result_Succ_Num");
                log.debug("短信结果捞取情况统计-总发送数量：" + Send_Num + " 有结果数量：" + Result_Num + " 无结果数量：" + No_Result_Num + " 成功结果数量：" + Result_Succ_Num);

                if (No_Result_Num == 0) {
                    //更新UCIS_CJ_result记录  删除开关
                    String[] data2 = new String[3];
                    data2[0] = "update UCIS_CJ_result set sms_success_count='" + Result_Succ_Num + "' where cj_guid='" + sms_guid + "'";
                    data2[1] = "delete from ucis_sms_send where type_id='A00015' and sms_guid='" + sms_guid + "'";
                    data2[2] = "Update Cj_Comprehensive_Ucis Set Cj_Comprehensive_Ucis.Smsstatus    = Ucis_Proc_Sendsmsresult.Smsstatus,Cj_Comprehensive_Ucis.Sms_Sendtime = Ucis_Proc_Sendsmsresult.Send_Time From Cj_Comprehensive_Ucis Inner Join Ucis_Proc_Sendsmsresult	 On Cj_Comprehensive_Ucis.Doc_Guid = Ucis_Proc_Sendsmsresult.Send_Guid	 Where Cj_Comprehensive_Ucis.Sms_Guid = '" + sms_guid + "'	 And Ucis_Proc_Sendsmsresult.Sms_Guid = '" + sms_guid + "'";

                    log.debug("更新监控 处理SQL:" + data2.toString());
                    int[] dataresult2 = eliteBean.updateBatch(data2, "dataSource");
                    log.debug("更新监控 处理SQL结果:" + dataresult2.toString());

                    //未捞取结果的默认更新为失败
                    eliteBean.update("update DOCUMENTOUT_UCIS set smsstatus='SMSFAIL' WHERE sms_guid ='" + sms_guid + "' AND smsstatus IS NULL And TRIALCOUNT=TRIALCOUNTLIMIT", "dataSource");

                    eliteBean.update("update CJ_Comprehensive_UCIS set smsstatus='SMSFAIL' WHERE sms_guid ='" + sms_guid + "' AND smsstatus IS NULL", "dataSource");

                    //更新监控 短信捞取结果结束时间
                    eliteBean.update("update ucis_sms_monitoring set smsresult_endtime=GETDATE() where SMS_GUID='" + sms_guid + "'", "dataSource")
                    eliteBean.update("insert into ucis_data_to_44 values('" + sms_guid + "','A00015',GETDATE())", "dataSource");
                    eliteBean.update("delete from Ucis_Proc_Sendsmsresult where Sms_Guid = '" + sms_guid + "'", "dataSource");
                }

            } else {
                log.debug("当前无短信账单短信结果捞取批次任务!");
            }
        } catch (Exception e) {
            log.error("result异常信息：", e);
        }
        return 1;
    }
}